import 'package:courseapp/entity/SignRecord.dart';
import 'package:courseapp/entity/coursedto.dart' as Course ;
import 'package:courseapp/entity/oncecoursedto.dart';
import 'package:courseapp/http/HttpUtils.dart';
import 'package:courseapp/page/teacher/compont/StudentSignRecord.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class TeacherTodayCourse extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TeacherTodayState();
  }

}

class TeacherTodayState extends State{

  List<Course.DataBean> courseList = [];
  Map week = {2:"周一",3:"周二",4 : "周三",5: "周四",6 :"周五"};

@override
  void initState() {
    // TODO: implement initState
  initRequest();
  super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListView.builder(itemBuilder:createWidget,
      itemCount: courseList.length,
      itemExtent: 50,
    );
  }




  Widget createWidget(BuildContext context,int index){
    Course.DataBean dataBean = courseList[index];
    String courseName = dataBean.courseName;
    String weekDay = week[dataBean.whatDay];
    return FlatButton(
        color: Colors.white,
        padding: EdgeInsets.all(0),
        onPressed: (){
         skipSignRecord(dataBean.id);
        },
        child: DecoratedBox(
            decoration: BoxDecoration( color: Colors.white, border: Border(bottom: Divider.createBorderSide(context, color: Colors.grey),)),
            child:Container(padding: EdgeInsets.all(5) ,child: Column(children: <Widget>[
              Container(alignment: Alignment.centerLeft, child:Text("课程:$courseName")),
              Container(alignment: Alignment.centerLeft, child:Text("时间:$weekDay")),],
            )
            )
        )

    );
  }

  Future<void> skipSignRecord(int id) async {
     Oncecoursedto oncecoursedto = await HttpUtil.getInstance().queryCourse(id);
     bool canSign =  oncecoursedto.data.canSign;
     //如果没有签到，那么开始签到，如果签到了，结束签到
     if(canSign) {
       createStopSignDialog(id);
     }else {
       createStartSignDialog(id);
     }
  }

  Future<void> initRequest() async {
     Course.Coursedto coursedto = await HttpUtil.getInstance().postTodayTeacherCourse();
     courseList.addAll(coursedto.data);
     setState(() {});
  }

  void createStopSignDialog(int courseId) {
    showDialog(context: context,child: Container(alignment: Alignment.center,child:
    SizedBox(
      width: 100,
      height:100,
      child: FlatButton(
        child: Text("结束签到",style: TextStyle(color: Colors.white),),
        color: Colors.black,
        shape: CircleBorder(
          side: BorderSide(color: Colors.black),
        ),
        onPressed: () async {
          await HttpUtil.getInstance().postTeacherSign(courseId, false);
          Navigator.of(context).pop();
          SignRecord signRecord = await HttpUtil.getInstance().queryNewestSign(courseId);
          StudentSignState.teacherSignId = signRecord.data.teacherSignId;
          Navigator.of(context).push(new MaterialPageRoute(builder: (context){ return StudentSignRecord(); }));
        },
      ),
    )
    ));
  }

  void createStartSignDialog(int courseId) {
    showDialog(context: context,child: Container(alignment: Alignment.center,child:
    SizedBox(
      width: 100,
      height:100,
      child: FlatButton(
        child: Text("开始签到",style: TextStyle(color: Colors.white),),
        color: Colors.pink,
        shape: CircleBorder(
          side: BorderSide(color: Colors.pink),
        ),
        onPressed: () async {
           await HttpUtil.getInstance().postTeacherSign(courseId, true);
           Navigator.of(context).pop();

        },
      ),
    )
    ));
  }


}
